import {
    Component,
    Injector,
    ViewEncapsulation,
    ViewChild,
    ElementRef,
    OnInit
} from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    ComboboxItemDto,
    SideTaskServiceProxy,
    ReceiveTaskInput
} from '@shared/service-proxies/service-proxies';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import * as moment from 'moment';
import { BatchIntoProduceComponent } from '../maintasks/batch-into-produce.component';
import { PutIntoProductionModalComponent } from '@app/plan/maintasks/put-into-production-modal.component';
import { MenuItem } from 'primeng/api';

@Component({
    templateUrl: './sidetask.component.html',
    styleUrls: ['./sidetask.component.less'],
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class SidetaskComponent extends AppComponentBase implements OnInit {
    // @ViewChild('boundingTeamGroupModal') boundingTeamGroupModal: BoundingTeamGroupModalComponent;
    @ViewChild('putIntoProductionModal')
    putIntoProductionModal: PutIntoProductionModalComponent;

    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    @ViewChild('dateTimePicker') dateTimePicker: ElementRef;

    @ViewChild('orderCategoryCombobox') orderCategoryCombobox: ElementRef;
    @ViewChild('statusCombobox') statusCombobox: ElementRef;
    @ViewChild('teamGroupCombobox') teamGroupCombobox: ElementRef;
    @ViewChild('batchIntoProduceModal') batchIntoProduceModal: BatchIntoProduceComponent;

    tabType = 1;
    statuses: ComboboxItemDto[];
    teamGroups: ComboboxItemDto[];
    en: any;
    filterText = '';
    advancedFiltersAreShown = false;
    isReceived = true;
    isCompleted = false;
    tabs: MenuItem[];

    constructor(
        injector: Injector,
        private _sideTaskServiceProxy: SideTaskServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {
        this._sideTaskServiceProxy
            .getComboxItemsForSearch()
            .subscribe(result => {
                this.statuses = result.statuses;
                this.teamGroups = result.teamGroups;
            });
        this.getTabs();
        // this.en = {
        //   firstDayOfWeek: 0,
        //   monthNames: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        //   monthNamesShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        //   dateFormat: 'yy-mm',
        //   weekHeader: 'Wk'
        // };
    }
    getMonths(event?: LazyLoadEvent, se?: number) {
       // console.log(se);
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this.primengTableHelper.showLoadingIndicator();
        this._sideTaskServiceProxy
            .getSideTasks(
                this.filterText,
                this.isReceived,
                undefined,
                this.isCompleted,
                58,
                this.primengTableHelper.getSkipCount(this.paginator, event),
                this.primengTableHelper.getMaxResultCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    receiveTask(id: number) {
        this.message.confirm('确认接收该条任务吗？', isConfirmed => {
            if (isConfirmed) {
                let rt = new ReceiveTaskInput();
                rt.id = id;
                // rt.recipientUserId = 2;
                this._sideTaskServiceProxy.receiveTask(rt).subscribe(() => {
                    this.notify.success('接收成功');
                    this.getMonths();
                });
            }
        });
    }
    deleteModel(id: number) {
        this.message.confirm('确认删除该条型号计划吗？', isConfirmed => {
            if (isConfirmed) {
                this._sideTaskServiceProxy.deleteSideTask(id).subscribe(() => {
                    this.notify.success(this.l('SuccessfullyDeleted'));
                    this.getMonths();
                });
            }
        });
    }
    getTabs() {
        this.tabs = [
            { label: '已接收', icon: 'fa fa-fw fa-bar-chart' },
            { label: '未接收', icon: 'fa fa-fw fa-calendar' },
            // { label: '已完成', icon: 'fa fa-fw fa-calendar' }
        ];
    }
    tabTypeClick(tab: any) {
        //console.log(tab);
        this.isCompleted = false;
        this.isReceived = true;
        if (tab === '已接收') {
            this.isReceived = true;
        } else if (tab === '未接收') {
            this.isReceived = false;
        // } else if (tab === '已完成') {
        //     this.isCompleted = true;
         }
        this.getMonths();
    }
    // importMonthOrders() {
    //   this.message.confirm(
    //     '确定同步该月的月度计划吗？',
    //     moment(this.selectedDate).year() + '年' + (moment(this.selectedDate).month()) + '月',
    //     isConfirmed => {
    //       if (isConfirmed) {
    //         this.anyncing = true;
    //         this._sideTaskServiceProxy.importMonthOrdersRef(
    //           moment(this.selectedDate).year(),
    //           moment(this.selectedDate).month()
    //         )
    //           .pipe(finalize(() => { this.anyncing = false; }))
    //           .subscribe(() => {
    //             this.notify.success('同步月度计划成功');
    //             this.getMonths();
    //           });
    //       }
    //     }
    //   );
    // }
}
